<template>
  <div>
    <!-- 这里是·修改密码的弹窗 -->
    <el-dialog
      append-to-body
      title="修改密码"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
      center
      @beforClose="beforClose"
      @close="newremove"
    >
      <div class="reset-main">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" class="reset-main-form">
          <el-form-item prop="pass">
            <el-input v-model="ruleForm.pass" type="password" placeholder="请输入新密码" autocomplete="off" />
          </el-form-item>
          <el-form-item prop="checkPass">
            <el-input v-model="ruleForm.checkPass" type="password" placeholder="请再次输入新密码" autocomplete="off" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="confirm"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 修改密码接口
import { changePassword } from '@/api/user'
export default {
  props: {
    dialogVisible: Boolean
  },
  data() {
    // 验证函数
    var validatePass = (rule, value, callback) => {
      value = value.trim()
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        this.ruleForm.pass = this.ruleForm.pass.trim()
        callback()
      }
    }
    var validatePassLength = (rule, value, callback) => {
      value = value.trim()
      if (value.length > 5 && value.length < 21) {
        this.ruleForm.pass = this.ruleForm.pass.trim()
        callback()
      } else {
        callback(new Error('密码由6-20位字母或数字组合'))
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (this.ruleForm.checkPass.trim() !== this.ruleForm.pass.trim()) {
        console.log(value.trim())
        console.log(this.ruleForm.pass.trim())
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        pass: '',
        checkPass: ''
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' },
          { validator: validatePassLength, trigger: 'blur' }
          // { min: 6, max: 16, message: '密码由6-20位字母或数字组合', trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }

    }
  },
  methods: {
    // 点击修改密码
    async confirm() {
      // 第一步校验表单
      try { await this.$refs.ruleForm.validate() } catch (e) {
        // console.log('eeee', e)
        return
      }
      // 拿到数据 调用接口
      const id = JSON.parse(localStorage.getItem('userinfo')).id

      const res = await changePassword({ id, newpassword: this.ruleForm.checkPass })
      console.log(res)
      if (res.data.code === 0) {
        this.$message.error(res.data.msg)
      } else if (res.data.code === 1) {
        this.$message.success(res.data.msg)
      } else {
        this.$message.error(res.data.msg)
      }
      // console.log('res', res)
      // this.$message.success('密码修改成功')
      // 关闭弹窗
      this.dialogVisible = false
    },
    beforClose() {
      // 给父组件传递信息
      this.$emit('dialogVisibles', false)
    },
    // 点击关闭弹窗触发
    newremove() {
      // 清除表单数据和验证规则
      this.ruleForm = {
        pass: '',
        checkPass: ''
      }
      this.$refs.ruleForm.clearValidate()
      // 给父组件传递信息
      this.$emit('dialogVisibles', false)
    },
    handleClose(done) {
      done()
    }
  }
}
</script>

<style lang="scss" scoped>
.reset-main {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .reset-main-form {
    width: 400px;
    padding: 20px 70px;
  }
}
.dialog-footer{
    display: flex;
    justify-content: flex-end;
}
</style>
